import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
import scipy.stats.kde as kde

'''
test max_p in pysal with synthetic cancer data
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list():
    #print getCurTime()
    sKey = np.array([])
    fn = unitCSV    # input csv location
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        temp = np.array([])
        for i in range(2,1003):
            temp = np.append(temp, int(float(record[ra.fieldnames[i]])))
        #temp = [int(float(record["ID"])), int(float(record[cancerFN])), int(float(record[popFN])), float(record[cancerFN])/float(record[popFN]), 0.0]
        sKey = np.append(sKey, temp)

    sKey.shape = (1000,-1)
    #print sKey[:,1]
    #print getCurTime()
    #print sKey
    return sKey

def calOriRate(unit_cancer, unit_pop):
    sKey = np.array([])
    i = 0
    for item in unit_cancer:
        sKey = np.append(sKey, float(unit_cancer[i]/unit_pop[i]))
        i += 1
    #sKey.shape = (-1,1)
    return sKey

def build_smooth_list(inputCSV):
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    temp_smoothed_rate = np.array([])

    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        #unit_attri[i,4] = int(float(record[ra.fieldnames[-1]]))
        temp_smoothed_rate = np.append(temp_smoothed_rate, float(record["SmoothedRate"]))
    temp_smoothed_rate.shape = (-1,1)
    return temp_smoothed_rate


def dictolist(temp_dict):
    region = np.array([])
    for k, v in temp_dict.iteritems():
        region = np.append(region, [int(k), v])
    region.shape = (-1, 2)
    region = region[np.argsort(region[:,0]),:]
    return region
    
        
#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    #inputs = get_inputs()
    print "begin at " + getCurTime()
    
    unitCSV = "C:/TP1000_1m.csv"
    popFN = "pop"
    
    #filePath = "C:/_DATA/CancerData/test/Jan15/Thousand/10000/OO_CLK/"
    #filePath = "F:/OO_WARD/"
    dataMatrix = build_data_list()  # [pop, cancer1, cancer2, cancer3]
    unit_pop = dataMatrix[:,0]
    #unit_pop.shape = (-1,1)

    j = 0
    while j < 1:
        if j == 0:
            soMethod = "OO"
        elif j == 1:
            soMethod = "SO"
        elif j == 2:
            soMethod = "SS"
        else:
            print "error!"
            break

        filePath = "C:/_DATA/CancerData/test/Jan15/Thousand/6000/"+ soMethod +"_WARD/"
        repeat = 4

            
        #while repeat < 1001:
        while repeat < 5:
            regionCSV = filePath + soMethod +"_WARDcancer"+ str(repeat) +".csv"
            sField_Ob = "cancer" + str(repeat)
            #unit_attri = build_unit_list(sField_Ob) # [FID, cancer, pop, cancer/pop, smoothed_rate]
            
            
            unit_cancer = dataMatrix[:,repeat]
            #unit_cancer.shape = (-1,1)

            original_rate = calOriRate(unit_cancer, unit_pop)
            #smoothed_rate = build_smooth_list(regionCSV)
            print kde.gaussian_kde(original_rate)

         
            
                   
            repeat = repeat + 1

        j = j + 1
    print "end at " + getCurTime()
    print "========================================================================"  
